<?php
/*
This sends a little bit of JSON to say if the user is logged in. 
*/

require_once( 'core.php' );
require_once( 'current_user_api.php' );
require_once( 'string_api.php' );
require_once( 'file_api.php' );
require_once( 'bug_api.php' );
require_once( 'custom_field_api.php' );
require_once( 'collapse_api.php' );
require_once( 'relationship_api.php' );
require_once( 'project_api.php' );

auth_ensure_user_authenticated( );
//helper_begin_long_process( );

$t_plugin_path = config_get( 'plugin_path' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'ProjectsMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'TableMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'JSONstore.php' );

$authHashPrefix = plugin_config_get( 'authHashPrefix' );
$refineHumanURI = plugin_config_get( 'refineBaseURI' ); //for human linking
$refineURI = plugin_config_get( 'refineLocalURI' );
if(!$refineURI){
	$refineURI = plugin_config_get( 'refineBaseURI' );
}

# extracts the user information for the currently logged in user
# and prefixes it with u_
$f_user_id = gpc_get_int( 'id', auth_get_current_user_id() );
$row = user_get_row( $f_user_id );

extract( $row, EXTR_PREFIX_ALL, 'u' );
$u_email = user_get_email( $u_id );
$u_realname = user_get_realname( $u_id );



//Needed for making an issue
$t_project_id = helper_get_current_project();
	

$projectsMetadataObj = new ProjectsMetadata;
$projectsMetadataObj->authHashPrefix = $authHashPrefix;
$projectsMetadataObj->refineURI = $refineURI;
$projectsMetadataObj->refineHumanURI = $refineHumanURI;
$projectsMetadataObj->fallBack_issueProject_id = $t_project_id;
$projectsMetadataObj->fallBack_issueUserID = auth_get_current_user_id();
$projectsMetadataObj->fallBack_issueProfile_id = gpc_get_int( 'profile_id', 0 );

$projectsMetadataObj->get_gr_projects();
$projectsMetadataObj->get_only_projectsdata();
$projectsMetadataObj->update_current_projects();

$currentProjectDatasets = false;
$allDatasets = $projectsMetadataObj->projectsMetadata;
if(is_array($allDatasets)){
	if(array_key_exists($t_project_id, $allDatasets)){
		$currentProjectDatasets = $allDatasets[$t_project_id];
	}
	elseif($t_project_id == 0){
		$currentProjectDatasets = array();
		foreach($allDatasets as $issuesProjID => $actProjectArray){
			foreach($actProjectArray as $GRprojectID => $projectDataset){
				$currentProjectDatasets[$GRprojectID] = $projectDataset;
			}
		}
	}
}

html_page_top( "All Datasets Needing Documentation" );

?>

<div class="data-doc-page-body">
	
	<!-- page heading information -->
	<div class="data-doc-page-heading">
		<div class="data-doc-page-heading-row">
			<div class="data-doc-page-heading-title">
				<h2>Dataset Issue and Documentation Tracking</h2>
			</div>
			<div class="data-doc-page-heading-dset">
				<div class="data-doc-page-heading-dset-tab">
					<div class="data-doc-page-heading-dset-row">
						<div class="data-doc-page-heading-dset-title">
							<?php
							/*
							if($t_project_id == 0){
								echo echo "<h3>All datasets, for all projects</h3>";
							}
							else{
								
							}
							*/
							$actProjectName =  project_get_name($t_project_id);
							echo "<h3>Datasets assoiated with &ldquo;".$actProjectName."&rdquo;</h3>"; 
							
							?>
						</div>
						<div class="data-doc-page-heading-dset-des">
						The issue tracker currently monitors
						
						<?php
						
							$actDataSetCount = 0;
							echo " <span id=\"dataset-row-count\">";
							if(is_array($currentProjectDatasets)){
								$actDataSetCount = count($currentProjectDatasets);
								echo $actDataSetCount;
							}
							else{
								echo "0";
							}
							echo "</span>";
						
							if($t_project_id == 0){
								echo " datasets in all of the issue-tracker's projects. ";
							}
							else{
								echo " datasets in the current project. ";
							}
							
							if($actDataSetCount>0){
								//echo "Click on one of the tracked datasets linked below to view summary status information, and begin to resolve issues an add needed documentation. ";
							}
							
							if(is_array($projectsMetadataObj->unTrackedProjects)){
								echo "<br/><br/>";
								echo "Currently, Google Refine has <span id=\"grNotTrackCount\">".count($projectsMetadataObj->curGoogleRefineProjects)."</span> datasets not yet tracked for issues or data documentation.";
							}
							
						?>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<!-- Main content for this dataset tracking page -->	
	<div class="data-doc-page-primary">
		<?php
		if(is_array($currentProjectDatasets)){
			echo '<h3>Datasets Currently Tracked</h3>'.chr(13);
			echo "<p>The list below indicates datasets currently being monitored by the issue-tracker. Click on a dataset below to resolve open issues and work on its documentation. You can also add a new dataset by uploading to <a href=\"".$refineHumanURI,"\">Google Refine</a>. </p>";
			echo '<ul id="trackedProjects">'.chr(13);
			foreach($currentProjectDatasets as $GRprojectID => $projectDataset){
				
				if(isset($projectDataset["name"])){
					$currentName = $projectDataset["name"];
				}
				else{
					$currentName = $projectDataset["originalName"];
				}
				
				echo "<li>";
				
				echo "<a href=\"../plugin.php?page=DataRefine/data-doc-summary&grProject=".$GRprojectID."\"><strong>".$projectDataset["name"]."</strong></a>";
				echo "</li>".chr(13);
			}
			echo '</ul>'.chr(13);
		}
		else{
			if($actDataSetCount == 0){
				if($t_project_id == 0){
					echo '<h3>No Datasets Tracked</h3>'.chr(13);
					echo "<p>Add a new dataset by uploading to <a href=\"".$refineHumanURI,"\">Google Refine</a>. </p>";
				}
				else{
					echo '<h3>No Datasets Tracked in the Current Issue-Tracker Project</h3>'.chr(13);
					echo "<p>To find a monitore dataset, you may need to change projects in the issue-tracker (the pull-down menu in the upper right). You may also add a new dataset by uploading to <a href=\"".$refineHumanURI,"\">Google Refine</a>. </p>";
				}
				
			}
			echo '<ul id="trackedProjects"></ul>'.chr(13);
		}
		?>

	<br/>
	<br/>
		<?php
		if(is_array($projectsMetadataObj->unTrackedProjects)){
			
			echo "<h3>Google Refine Has <span id=\"grNotTrackCount\">".count($projectsMetadataObj->curGoogleRefineProjects)."</span> Projects Not Yet Tracked</h3>".chr(13);
			echo "<ul id=\"grProjects\" >".chr(13);
			
			foreach($projectsMetadataObj->unTrackedProjects as $GRprojectID => $projectDataset){
				
				echo "<li id=\"".$GRprojectID."_ut\">";
				
				echo "<strong>".$projectDataset["name"]."</strong> ";
				echo "(Last Modified: ".date("F j, Y, g:i a", strtotime($projectDataset["modified"])).") ";
				echo "[<a href=\"javascript:trackNewProject('".$GRprojectID."')\">Track this Project</a>]";
				echo "</li>".chr(13);
			}
			
			echo "</ul>".chr(13);
		}
		?>
	</div>
</div>

	

<?php
html_page_bottom();
?>
